﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using UDS.Components;

namespace UDS.Entity
{
    /// <summary>
    /// The data for a document (table UDS_Flow_Style_Data).
    /// </summary>
    public class FlowStyleData
    {
        private Dictionary<string, string> _dic;

        #region properties
        public int DocID
        {
            get;
            set;
        }

        public string A
        {
            get { return GetValue("A"); }
            set { _dic["A"] = value; }
        }

        public string B
        {
            get { return GetValue("B"); }
            set { _dic["B"] = value; }
        }

        public string C
        {
            get { return GetValue("C"); }
            set { _dic["C"] = value; }
        }

        public string D
        {
            get { return GetValue("D"); }
            set { _dic["D"] = value; }
        }

        public string E
        {
            get { return GetValue("E"); }
            set { _dic["E"] = value; }
        }

        public string F
        {
            get { return GetValue("F"); }
            set { _dic["F"] = value; }
        }

        public string G
        {
            get { return GetValue("G"); }
            set { _dic["G"] = value; }
        }

        public string H
        {
            get { return GetValue("H"); }
            set { _dic["H"] = value; }
        }

        public string I
        {
            get { return GetValue("I"); }
            set { _dic["I"] = value; }
        }

        public string J
        {
            get { return GetValue("J"); }
            set { _dic["J"] = value; }
        }

        public string K
        {
            get { return GetValue("K"); }
            set { _dic["K"] = value; }
        }

        public string L
        {
            get { return GetValue("L"); }
            set { _dic["L"] = value; }
        }

        public string M
        {
            get { return GetValue("M"); }
            set { _dic["M"] = value; }
        }

        public string N
        {
            get { return GetValue("N"); }
            set { _dic["N"] = value; }
        }

        public string O
        {
            get { return GetValue("O"); }
            set { _dic["O"] = value; }
        }

        public string P
        {
            get { return GetValue("P"); }
            set { _dic["P"] = value; }
        }

        public string Q
        {
            get { return GetValue("Q"); }
            set { _dic["Q"] = value; }
        }

        public string R
        {
            get { return GetValue("R"); }
            set { _dic["R"] = value; }
        }

        public string S
        {
            get { return GetValue("S"); }
            set { _dic["S"] = value; }
        }

        public string T
        {
            get { return GetValue("T"); }
            set { _dic["T"] = value; }
        }

        public string U
        {
            get { return GetValue("U"); }
            set { _dic["U"] = value; }
        }

        public string V
        {
            get { return GetValue("V"); }
            set { _dic["V"] = value; }
        }

        public string W
        {
            get { return GetValue("W"); }
            set { _dic["W"] = value; }
        }

        public string X
        {
            get { return GetValue("X"); }
            set { _dic["X"] = value; }
        }

        public string Y
        {
            get { return GetValue("Y"); }
            set { _dic["Y"] = value; }
        }

        public string Z
        {
            get { return GetValue("Z"); }
            set { _dic["Z"] = value; }
        }

        /// <summary>
        /// Get the value for the given field.
        /// </summary>
        public string this[string field]
        {
            get
            {
                if (string.IsNullOrEmpty(field))
                {
                    throw new ArgumentNullException("field");
                }
                return GetValue(field);
            }
        }
        #endregion

        private FlowStyleData()
        {
            _dic = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
        }

        internal static FlowStyleData ReadFlowStyleData(IDataReader dr, int docId)
        {
#if DEBUG
            int id = (int)dr["Doc_ID"];
            Debug.Assert(id == docId);
#endif

            FlowStyleData d = new FlowStyleData();
            d.DocID = docId;

            d._dic.Clear();
            for (int i = 'A'; i <= 'Z'; i++)
            {
                string name = new string((char)i, 1);
                d._dic.Add(name, ReadString(dr, name));
            }

            return d;
        }

        private string GetValue(string field)
        {
            string value;
            if (_dic.TryGetValue(field, out value))
            {
                return value;
            }
            return null;
        }

        private static string ReadString(IDataReader dr, string columnName)
        {
            object obj = dr[columnName];
            if (obj == null || obj == DBNull.Value)
            {
                return null;
            }
            return (string)obj;
        }
    }
}
